<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class News extends MY_Controller {
	private $list_cat;
	public function __construct(){
		parent::__construct();
		$this->list_cat = $this->cm->get_all($this->tbl_cat,array('type'=>'article'),'arr');
		$this->load->model('news_model','news');
		
		$this->_data['nav_news'] = ' c ';
	}
		
	public function index()
	{
		$this->_data['list_news'] = $this->news->get_by_page();		
		
		//current menu
		$this->_data['menu_list'] = ' cm ';
		$this->load->view('news/index',$this->_data);
	}
	
	function add(){
	    
        $this->_data['cat_select_tree'] = cat_select_tree($this->list_cat);
		$this->load->view('news/add',$this->_data);
		
	}
	
	
	
	
	function cat(){

		//导航名称
		$this->_data['now_pos'] = '新闻管理 &gt; 类别列表';
		getCatTree($this->list_cat,0,$list_rs);
		$this->_data['list_cat'] = $list_rs;
		
		//current menu
		$this->_data['menu_cat'] = ' cm ';
		
		$this->load->view('news/cat',$this->_data);
	}
	
	function cat_add(){
		
		echo $this->load->view('news/ajax_cat_add',$this->_data,true);
	}
	
	function cat_sub_add(){
		if(empty($this->c_data['id'])) die;
		$this->_data['pid'] = $this->c_data['id'];
		
		echo $this->load->view('news/ajax_cat_add',$this->_data,true);
	}
	
	function cat_edit(){
		
		if(empty($this->c_data['id'])) die;
		$cat = $this->cm->get_by_id($this->tbl_cat,$this->c_data['id']);
		$this->_data['cat'] = $cat;
		$this->_data['pid'] = $cat->pid;
		
		echo $this->load->view('news/ajax_cat_add',$this->_data,true);
		
	}
	
	function cat_del(){
		if(empty($this->c_data['id'])) die;
		if(hasCatSub($this->list_cat, $this->c_data['id'])) ejd('','只能删除叶子类别！');
		$this->cm->delete($this->tbl_cat,array('id'=>$this->c_data['id']));
		ejd('','','reload');
	}
	
	function del(){
		if(empty($this->c_data['id'])) die;
		$this->cm->delete($this->tbl_news,array('id'=>$this->c_data['id']));
		ejd('','','reload');
	}
	
	function cat_add_do(){
		
		if(!$this->form_validation->required($this->c_data['title'])) die;
		$flag = $this->cm->save($this->tbl_cat,$this->c_data);
		
		if($flag){
			ejd('','','reload');
		}else{
			ejd('fail','失败！');
		}
		
	}
	
	function add_do(){
	
		if(!$this->form_validation->required($this->c_data['title'])) die;
		if(!$this->form_validation->required($this->c_data['cid'])) die;
		if(!$this->form_validation->required($this->c_data['content'])) die;
		
		$flag = $this->cm->save($this->tbl_news,$this->c_data);
	
		if($flag){
			ejd('','','reload');
		}else{
			ejd('fail','失败！');
		}
	
	}
	
	function del_batch(){
		if(empty($this->c_data['ids'])) die;
		$this->cm->delete_batch($this->tbl_news,$this->c_data['ids']);
		ejd('','','reload');
	}
	
	function edit(){
	
		if(empty($this->c_data['id'])) die;

		$this->_data['cat_select_tree'] = cat_select_tree($this->list_cat);
		
		$news = $this->cm->get_by_id($this->tbl_news,$this->c_data['id']);
		$this->_data['news'] = $news;
		
		echo $this->load->view('news/add',$this->_data,true);
	
	}
		
	
}
